#include "EditProcedure.h"
#include <EditEKETestCase.h>
#include <EditTRDPTestCase.h>
#include <QFileDialog>
#include <QStandardItemModel>
#include<qdebug.h>
#include"EditTRDPTestCase.h"
#include <Tool/excelhandler.h>
EditProcedure::EditProcedure(QWidget *parent)
	: QWidget(parent)
{
	ui.setupUi(this);
    editEke = new EditEKETestCase();
    editTrdp = new EditTRDPTestCase();
	connectAllSlots();
	initControls();
}

EditProcedure::~EditProcedure()
{}

void EditProcedure::SetTestCaseName(QString strName)
{
    ui.lineEdit_testCaseName->setText(strName);
}

void EditProcedure::connectAllSlots()
{
	connect(ui.pushButton_create, &QPushButton::clicked, this, &EditProcedure::onButtonCreateCliccked);
    connect(editTrdp, &EditTRDPTestCase::OnModifyFinish, this, &EditProcedure::OnTRDPModifyFinish);
	
}

void EditProcedure::initControls()
{
	ui.comboBox_procedureType->addItem("EKE");
	ui.comboBox_procedureType->addItem("TRDP");
	ui.comboBox_procedureType->addItem("MVB");

}

//保存数据到excel
//COMID|CYCLETIME|SOURCEIP|DESTIP|VARIANT_COMBINE
void EditProcedure::SaveModifyResultToExcel(QString strExclePath)
{

}

//按行读取编辑结果
//COMID|CYCLETIME|SOURCEIP|DESTIP|VariantName_Value
void EditProcedure::LoadModifyResultFromExcel(QString strExclePath)
{

}

void EditProcedure::onButtonCreateCliccked()
{
	QString strType = ui.comboBox_procedureType->currentText();
	if (strType =="EKE")
	{


//        editEke->setWindowModality(Qt::ApplicationModal); //设置阻塞类型
//		editEke->show();

	}
	else if (strType=="TRDP")
	{
        editTrdp->setWindowModality(Qt::ApplicationModal); //设置阻塞类型
		editTrdp->show();

	}
	else
	{


	}

}

void EditProcedure::ShowTRDPModifyResult()
{

    int nTableRow = TrdpModifyResultList.size();
    qDebug()<<"tableRow:"<<nTableRow<<"\n";
    int nTableColumn =7;
    //tableview显示所有编辑的内容
    QStandardItemModel* model = new QStandardItemModel();
    /* 设置列数 */
    model->setColumnCount(nTableColumn);
    /* 设置行数 */
    model->setRowCount(nTableRow);

    /* 设置列宽在可视界面自适应宽度 */
    ui.tableView->horizontalHeader()->setResizeContentsPrecision(QHeaderView::Stretch);
    /* 行颜色交替显示 */
    ui.tableView->setAlternatingRowColors(true);
    /* 不允许在图形界面修改内容 */
    ui.tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);

    /* 在表格内加入内容 */
    QList<QList<QStandardItem*>> rows;
    for (int i = 0; i < nTableRow; ++i)
    {
        QList<QStandardItem*> row;

        QStandardItem* item1 = new QStandardItem(QString::number(TrdpModifyResultList[i].ComID));
        row.append(item1);
        QStandardItem* item2 = new QStandardItem(QString::number(TrdpModifyResultList[i].cycleTime));
        row.append(item2);
        QStandardItem* item3 = new QStandardItem(TrdpModifyResultList[i].strSourceIP);
        row.append(item3);
        QStandardItem* item4 = new QStandardItem(TrdpModifyResultList[i].strDestIp);
        row.append(item4);
        QString strCombine ;
        foreach(QString str,TrdpModifyResultList[i].VariantName_Value)
        {
            strCombine+=str;
            strCombine+=QString::fromLocal8Bit(";");
        }
        strCombine.chop(1);//去除最后的';'
        QStandardItem* item5 = new QStandardItem(strCombine);
        row.append(item5);
        rows.append(row);
    }

    model->clear();
    //设置表头名
    QStringList headList = QStringList();
    headList<<QString::fromLocal8Bit("ComID");
    headList<<QString::fromLocal8Bit("循环时间");
    headList<<QString::fromLocal8Bit("源IP");
    headList<<QString::fromLocal8Bit("目的IP");
    headList<<QString::fromLocal8Bit("变量信息");
    model->setHorizontalHeaderLabels(headList);
    qDebug()<<"itemSize:"<<rows.count()<<"\n";
    foreach (const auto& row,rows)
    {
        model->appendRow(row);
    }
    /* 显示表 */
    ui.tableView->setModel(model);

    ui.tableView->show();

}

void EditProcedure::OnTRDPModifyFinish()
{
    TrdpModifyResultList.append(editTrdp->GetTRDPModifyResult());
    ShowTRDPModifyResult();
}

void EditProcedure::on_pushButton_Load_clicked()
{
    QFileDialog dialog(nullptr, QString::fromLocal8Bit("选择文件夹并保存文件"), "", "xlsx Files (*.xlsx);;All Files (*)");
    dialog.setFileMode(QFileDialog::ExistingFile);
    dialog.setAcceptMode(QFileDialog::AcceptOpen);

    QString fileName;
    // 显示对话框并获取用户选择的文件路径
    if (dialog.exec()) {
        fileName = dialog.selectedFiles().first();
        // 在这里处理文件保存逻辑
        qDebug() <<QString::fromLocal8Bit( "选择的文件路径：" )<< fileName;
    }
    LoadModifyResultFromExcel(fileName);
    ShowTRDPModifyResult();
}

void EditProcedure::on_pushButton_clicked()
{
    QString strname=ui.lineEdit_testCaseName->text();
    emit changeTestcaseName(strname);
}

